
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta charset="utf-8"></meta>
      <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
      <title>15.2.&nbsp;Clone subgraph - Chapter&nbsp;15.&nbsp;Graph Refactoring</title>
      <link rel="stylesheet" type="text/css" href="../../docbook.css"></link>
      <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,400italic,600,300italic"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/theme/neo.min.css"></link>
      <link rel="stylesheet" type="text/css" href="../../css/chunked-base.css"></link>
      <link rel="stylesheet" type="text/css" href="../../css/extra.css"></link><script src="//code.jquery.com/jquery-1.12.4.js" type="text/javascript"></script><script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js" type="text/javascript"></script><script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/addon/runmode/runmode.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/mode/cypher/cypher.min.js" type="text/javascript"></script><script src="../../javascript/datatable.js" type="text/javascript"></script><script src="../../javascript/colorize.js" type="text/javascript"></script><script src="../../javascript/tabs-for-chunked.js" type="text/javascript"></script><script src="../../javascript/mp-nav.js" type="text/javascript"></script><script src="../../javascript/versionswitcher.js" type="text/javascript"></script><script src="../../javascript/version.js" type="text/javascript"></script><script src="//s3-eu-west-1.amazonaws.com/alpha.neohq.net/docs/new-manual/assets/search.js" type="text/javascript"></script><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"></meta>
      <link rel="prev" href="../clone-nodes/" title="15.1.&nbsp;Clone nodes"></link>
      <link rel="next" href="../merge-nodes/" title="15.3.&nbsp;Merge Nodes"></link>
      <link rel="shortcut icon" href="https://neo4j.com/wp-content/themes/neo4jweb/favicon.ico"></link><script>
        $(document).ready(function() {
          CodeMirror.colorize();
          tabTheSource($('body'));
          var $header = $('header').first();
          $header.prepend(
            $('<a href="" id="logo"><img src="https://neo4j.com/wp-content/themes/neo4jweb/assets/images/neo4j-logo-2015.png" alt="Neo4j Logo"></img></a>')
          );
          var $sidebar = $('<div id="sidebar-wrapper"></div>');
          $.get('toc.html', function (d){
            $(d).appendTo($sidebar);
            highlightToc();
            highlightLibraryHeader();
          });
          $sidebar.insertAfter($('header').first());
        });
        </script></head>
   <body>
      <header>
         <div class="searchbox">
            <form id="search-form" class="search" name="search-form" role="search"><input id="search-form-input" name="q" title="search" type="search" lang="en" placeholder="Search Neo4j docs..." aria-label="Search Neo4j documentation" max-length="128" required="required"></input><input id="search-form-button" type="submit" value="Search"></input></form>
         </div>
         <ul class="documentation-library">
            <li><a href="https://neo4j.com/docs/operations-manual/current">Operations Manual</a></li>
            <li><a href="https://neo4j.com/docs/developer-manual/current/">Developer Manual</a></li>
            <li><a href="https://neo4j.com/docs/ogm-manual/current/">OGM Manual</a></li>
            <li><a href="https://neo4j.com/docs/graph-algorithms/current/">Graph Algorithms</a></li>
            <li><a href="https://neo4j-contrib.github.io/neo4j-apoc-procedures/3.4/">APOC</a></li>
            <li><a href="https://neo4j.com/docs/java-reference/current/">Java Reference</a></li>
         </ul>
         <nav id="header-nav"><span class="nav-previous"><a accesskey="p" href="../clone-nodes/"><span class="fa fa-long-arrow-left" aria-hidden="true"></span>Clone nodes</a></span><span class="nav-current">
               <p class="nav-title hidden">15.2.&nbsp;Clone subgraph</p></span><span class="nav-next"><a accesskey="n" href="../merge-nodes/">Merge Nodes<span class="fa fa-long-arrow-right" aria-hidden="true"></span></a></span></nav>
      </header>
      <div id="search-results" class="hidden"></div>
      <section class="section" id="clone-subgraph">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title" style="clear: both"><a class="anchor" href="#clone-subgraph"></a>15.2.&nbsp;Clone subgraph
                  </h2>
               </div>
            </div>
         </div>
         <p>You can use the <code class="literal">cloneSubgraph()</code> and <code class="literal">cloneSubraphFromPaths()</code> to clone a subgraph defined either by a list of nodes and a list of relationships, or a list of paths.
            This is useful when you want to ensure the cloned subgraph isn&#8217;t connected to the original nodes, or to nodes outside the
            subgraph.
         </p>
         <p>If relationships are not provided, then all relationships between the given nodes will be cloned.</p>
         <p>In the config map, we can supply a <code class="literal">standinNodes</code> list (of pairs of nodes), allowing an existing node in the graph to act as a standin for another node in the cloned subgraph.
            This can be useful when you want to attach the cloned subgraph to another node in your graph (in place of cloning a node).
         </p>
         <p>For example, when cloning a tree from one root to another.</p>
         <p>Let&#8217;s create a dataset of two trees:</p><pre class="programlisting highlight"><code data-lang="cypher">CREATE  (rootA:Root{name:'A'}),
        (rootB:Root{name:'B'}),
        (n1:Node{name:'node1', id:1}),
        (n2:Node{name:'node2', id:2}),
        (n3:Node{name:'node3', id:3}),
        (n4:Node{name:'node4', id:4}),
        (n5:Node{name:'node5', id:5}),
        (n6:Node{name:'node6', id:6}),
        (n7:Node{name:'node7', id:7}),
        (n8:Node{name:'node8', id:8}),
        (n9:Node{name:'node9', id:9}),
        (n10:Node{name:'node10', id:10}),
        (n11:Node{name:'node11', id:11}),
        (n12:Node{name:'node12', id:12})
        CREATE (rootA)-[:LINK]-&gt;(n1)-[:LINK]-&gt;(n2)-[:LINK]-&gt;(n3)-[:LINK]-&gt;(n4)
        CREATE               (n1)-[:LINK]-&gt;(n5)-[:LINK]-&gt;(n6)&lt;-[:LINK]-(n7)
        CREATE                             (n5)-[:LINK]-&gt;(n8)
        CREATE                             (n5)-[:LINK]-&gt;(n9)-[:DIFFERENT_LINK]-&gt;(n10)
        CREATE (rootB)-[:LINK]-&gt;(n11)</code></pre><p>For our use case, we want to clone a subtree starting from rootA consisting only of outgoing :LINK relationships,
            and attach that subgraph to rootB. rootB becomes the standin for rootA (which is not cloned).
         </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH  (rootA:Root{name:'A'}),
        (rootB:Root{name:'B'})
MATCH path = (rootA)-[:LINK*]-&gt;(node)
WITH rootA, rootB, collect(path) as paths
CALL apoc.refactor.cloneSubgraphFromPaths(paths, {standinNodes:[[rootA, rootB]]}) YIELD input, output, error
RETURN input, output, error</code></pre><p>A subsequent MATCH to the entire graph will show the subgraph has been cloned and attached to rootB.</p>
         <p>We can instead use <code class="literal">apoc.refactor.cloneSubgraph()</code>, providing the lists of nodes and relationships which form the subgraph.
            We can get the nodes and rels from the yielded output of <code class="literal">apoc.path.subgraphAll()</code>, and we can filter to the relationship types we want in the call.
         </p>
         <p>After clearing and repopulating the tree graph, we can run:</p><pre class="programlisting highlight"><code data-lang="cypher">MATCH  (rootA:Root{name:'A'}),
        (rootB:Root{name:'B'})
CALL apoc.path.subgraphAll(rootA, {relationshipFilter:'LINK&gt;'}) YIELD nodes, relationships
CALL apoc.refactor.cloneSubgraph(nodes, [rel in relationships WHERE type(rel) = 'LINK'], {standinNodes:[[rootA, rootB]]}) YIELD input, output, error
RETURN input, output, error</code></pre><p>The resulting graph will be the same as our earlier <code class="literal">apoc.refactor.cloneSubgraphFromPaths()</code> call.
         </p>
      </section>
      <footer><script type="text/javascript">
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
          //Allow Linker
          ga('create', 'UA-1192232-34','auto', {'allowLinker': true});
          ga('send', 'pageview');
          // Load the plugin.
          ga('require', 'linker');
          // Define which domains to autoLink.
          ga('linker:autoLink', ['neo4j.org','neo4j.com','neotechnology.com','graphdatabases.com','graphconnect.com']);
        </script><script type="text/javascript">
          document.write(unescape("%3Cscript src='//munchkin.marketo.net/munchkin.js' type='text/javascript'%3E%3C/script%3E"));
        </script><script>Munchkin.init('773-GON-065');</script></footer>
   </body>
</html>